/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.log4j.Logger;

/**
 *
 * @author dell
 */
public class LoadTableMapData {

    static Logger logger = Logger.getLogger(LoadTableMapData.class);
    /*数据字典*/
    public static int DATA_MAP_NUM = 4;
    public static ArrayList<String> dataMapFields;//需要映射的列
    public static HashMap<String, HashMap<String, String>> dataMapHashMaps;//映射表

    public void init(String tablename) {
        //需要映射的配置文件
        dataMapFields = new ArrayList<String>(DATA_MAP_NUM);
        //映射配置文件中的数据列
        dataMapHashMaps = new HashMap<String, HashMap<String, String>>(DATA_MAP_NUM);

        String[] tablenames = tablename.split("\\|");
        for (int n = 0; n < tablenames.length; n++) {
            dataMapFields.add(tablenames[n]);
        }

        String line;
        String[] dvd;
        for (String field : dataMapFields) {
            HashMap<String, String> curMap = new HashMap<String, String>();
            try {
                //配置文件
                String mataFilePath = ClassLoader.getSystemClassLoader().getResource(field + ".mapdata").getFile();
                logger.info("the config mapdata is " + mataFilePath);
                BufferedReader br = new BufferedReader(new FileReader(mataFilePath));
                try {
                    while ((line = br.readLine()) != null) {
                        line = line.trim();
                        if (line.isEmpty()) {
                        } else {
                            dvd = line.split("\t");
                            if (dvd.length == 2) {
                                curMap.put(dvd[0], dvd[1]);
                            }
                        }
                    }
                    dataMapHashMaps.put(field, curMap);
                } catch (Exception ex) {
                    dataMapHashMaps = null;
                    logger.error("cannot read mapData file : " + field + ".mapdata. Cause " + ex.getMessage(), ex);
                } finally {
                    try {
                        br.close();
                    } catch (IOException ex) {
                    }
                }
            } catch (Exception ex) {
                dataMapHashMaps = null;
                logger.error("cannot find mapData file : " + field + ".mapdata. Cause " + ex.getMessage(), ex);
            }
            logger.info("load mataFilePath finish.");
        }
    }
}
